//
//  ProductCategoriesManage.m
//  AFNetworking
//
//  Created by admin on 2020/4/29.
//

#import "ProductCategoriesManage.h"
#import "ProductCategoriesManageCell.h"
@interface ProductCategoriesManage ()<UITableViewDelegate,UITableViewDataSource>

@property(strong, nonatomic) UITableView *tableView;

@property(strong, nonatomic) UIView *toolView;
@property(strong, nonatomic) UIButton *createBtn;
@property(strong, nonatomic) UIButton *editBtn;
@property(strong, nonatomic) UIButton *navEditBtn;

@property (nonatomic, strong) NSMutableArray * datalist;

@property(assign, nonatomic) BOOL isEdit;
@end

@implementation ProductCategoriesManage

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

-(void)configUI{
    self.title = @"分类";
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
    initWithCustomView:self.navEditBtn];
    [self.navigationItem setRightBarButtonItem:buttonItem];
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.toolView];
    [self.toolView addSubview:self.createBtn];
    [self.toolView addSubview:self.editBtn];
    
}
-(void)rightBarButtonItemClick:(UIButton*)sender{
    _isEdit = NO;
    [self.navEditBtn setHidden:YES];
    [self.tableView reloadData];
}
-(UIButton *)navEditBtn{
    if (!_navEditBtn) {
        _navEditBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
        [_navEditBtn setFrame:CGRectMake(0, 0, 30, 40)];
        [_navEditBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_navEditBtn setTitle:@"完成" forState:UIControlStateSelected];
        [_navEditBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        [_navEditBtn addTarget:self action:@selector(rightBarButtonItemClick:) forControlEvents:UIControlEventTouchUpInside];
         [self.navEditBtn setHidden:YES];
    }
    return _navEditBtn;

}

-(NSMutableArray *)datalist{
    if (!_datalist) {
        _datalist = [@[
            @{
                @"title":@"未分类",
                @"count":@"15"
            },
            @{
                @"title":@"奶茶",
                @"count":@"15"
            },
      
        ] mutableCopy];
    }
    return _datalist;
}

-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
        [_tableView setTableFooterView:[UIView new]];
    }
    return _tableView;
}
-(UIView *)toolView{
    if (!_toolView) {
        _toolView = [UIView new];
        _toolView.backgroundColor = UIColor.whiteColor;
        [_toolView ug_shadowColor:UIColor.blackColor width:1];
    }
    return _toolView;
}
-(UIButton *)createBtn{
    if (!_createBtn) {
        _createBtn = [UIButton new];
        [_createBtn setTitle:@"新建分类" forState:UIControlStateNormal];
        [_createBtn setTitleColor:[UIColor ug_hexString:@"#007AFF"] forState:UIControlStateNormal];
        [_createBtn ug_radius:4];
        _createBtn.titleLabel.font = FONT_SYS14;
        [_createBtn ug_borderColor:[UIColor ug_hexString:@"#007AFF"] width:1];
        UG_WEAKSELF
        [_createBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"新建分类" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                textField.ug_maximumLimit = 10;
                textField.placeholder = @"请填写分类名称";
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UITextField *nametf = alertController.textFields.firstObject;
                [weakSelf.view ug_msg:nametf.text];
                [weakSelf.datalist addObject:@{@"title":nametf.text}];
                [weakSelf.tableView reloadData];
            }];
            
            
            [alertController addAction:cancelAction];
            [alertController addAction:okAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }];
    }
    return _createBtn;
}
-(UIButton *)editBtn{
    if (!_editBtn) {
        _editBtn = [UIButton new];
        [_editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_editBtn ug_radius:4];
        _editBtn.titleLabel.font = FONT_SYS14;
        [_editBtn setBackgroundColor:[UIColor ug_hexString:@"#007AFF"]];
        [_editBtn setTitle:@"管理分类" forState:UIControlStateNormal];
        UG_WEAKSELF
        [_editBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
            weakSelf.isEdit = YES;
            [self.navEditBtn setHidden:NO];
            [weakSelf.tableView reloadData];
        }];
    }
    return _editBtn;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datalist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* indentifier = @"cell";
    ProductCategoriesManageCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[ProductCategoriesManageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    cell.editView.hidden = !_isEdit;
    cell.countLab.hidden = _isEdit;
    cell.rightArrowImg.hidden = _isEdit;
    NSDictionary *dic = [_datalist objectAtIndex:indexPath.row];
    NSString *titleStr = [dic objectForKey:@"title"];
    cell.textLabel.text = titleStr;
    cell.countLab.text =[NSString stringWithFormat:@"%@件商品",[dic objectForKey:@"count"]] ;
    if([titleStr isEqualToString:@"未分类"]){
        cell.textLabel.textColor = [UIColor ug_hexString:@"#FF5027"];
    }else{
        cell.textLabel.textColor = COLOR_33;
    }
        
    UG_WEAKSELF
    [cell.deleteBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
        [weakSelf.datalist removeObjectAtIndex:indexPath.row];
    }];
    __weak typeof(indexPath) weakindexPath = indexPath;
    [cell.upBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
        if (indexPath.row==0) {
            return ;
        }
        NSIndexPath *toindex = [NSIndexPath indexPathForRow:weakindexPath.row-1 inSection:weakindexPath.section];
        id data = [weakSelf.datalist objectAtIndex:indexPath.row];
        [weakSelf.datalist removeObject:data];
        [weakSelf.datalist insertObject:data atIndex:toindex.row];
        [weakSelf.tableView moveRowAtIndexPath:indexPath toIndexPath:toindex];
        [weakSelf bk_performBlock:^(id obj) {
            [weakSelf.tableView reloadData];
        } afterDelay:0.35];
        
    }];
    
    [cell.downBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
        if (indexPath.row==weakSelf.datalist.count) {
            return ;
        }
        NSIndexPath *toindex = [NSIndexPath indexPathForRow:weakindexPath.row+1 inSection:weakindexPath.section];
        id data = [weakSelf.datalist objectAtIndex:indexPath.row];
        [weakSelf.datalist removeObject:data];
        [weakSelf.datalist insertObject:data atIndex:toindex.row];
        [weakSelf.tableView moveRowAtIndexPath:indexPath toIndexPath:toindex];
        [weakSelf bk_performBlock:^(id obj) {
            [weakSelf.tableView reloadData];
        } afterDelay:0.35];
        
    }];
    [cell.deleteBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
        
        id data = [weakSelf.datalist objectAtIndex:indexPath.row];
        [weakSelf.datalist removeObject:data];
        [weakSelf.tableView deselectRowAtIndexPath:indexPath animated:YES];
        [weakSelf bk_performBlock:^(id obj) {
            [weakSelf.tableView reloadData];
        } afterDelay:0.35];
    }];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_isEdit) {
        return;
    }
    NSDictionary *dic = [_datalist objectAtIndex:indexPath.row];
    [self routepush:@"CategoriesProductListVC" param:@{@"title":[dic objectForKey:@"title"]}];
    
}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.toolView.mas_top);
    }];
    [_toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.height.mas_equalTo(KSAFE_Bottom+44);
    }];
    [_createBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.editBtn.mas_left).mas_offset(-10);
        make.top.mas_equalTo(self.toolView).mas_offset(10);
        make.size.mas_equalTo(CGSizeMake(80, 32));
    }];
    [_editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.toolView).mas_offset(-10);
        make.top.mas_equalTo(self.toolView).mas_offset(10);
        make.size.mas_equalTo(CGSizeMake(80, 32));
    }];
}

@end
